package com.xitang.system.controller;

import com.xitang.common.bean.BizMaterial;
import com.xitang.common.dto.MaterialQueryReq;
import com.xitang.common.dto.PageQueryRsp;
import com.xitang.common.dto.ResultVO;
import com.xitang.common.service.IMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "材料接口列表-系统")
@RestController
public class MaterialController extends BaseSystemController {
    @Autowired
    private IMaterialService materialService;

    @Value("${qiniu.domain}")
    private String domain;

    @ApiOperation(value = "获取材料列表信息", notes = "获取材料列表信息")
    @GetMapping("/material")
    public ResultVO<List<BizMaterial>> materialList(@RequestParam(name = "page", required = false) Integer page,
                                                    @RequestParam(name = "limit", required = false) Integer limit,
                                                    @RequestParam(name = "materialCode", required = false) String materialCode,
                                                    @RequestParam(name = "materialName", required = false) String materialName) {
        MaterialQueryReq queryReq = MaterialQueryReq.builder().pageNo(page).pageSize(limit).materialCode(materialCode).materialName(materialName).build();
        PageQueryRsp<BizMaterial> pageQueryRsp = materialService.getMaterialList(queryReq);
        return ResultVO.success(pageQueryRsp.getRecords(), pageQueryRsp.getTotal());
    }

    @ApiOperation(value = "获取材料详细信息", notes = "根据材料编码获取材料详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "materialCode", value = "材料编码", dataType = "String")
    })
    @GetMapping("/material/{materialCode}")
    public ResultVO<BizMaterial> materialDetail(@PathVariable("materialCode") String materialCode) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("domain", domain);
        return ResultVO.success(materialService.getMaterialDetail(materialCode), headers);
    }

    @ApiOperation(value = "更新材料详细信息", notes = "根据材料编码更新材料详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "materialCode", value = "材料编码", dataType = "String"),
            @ApiImplicitParam(paramType = "body", name = "material", value = "材料详情", dataTypeClass = BizMaterial.class)
    })
    @PutMapping("/material/{materialCode}")
    public ResultVO<BizMaterial> updateMaterialDetail(@PathVariable("materialCode") String materialCode, @RequestBody BizMaterial material) throws Exception {
        if (StringUtils.isEmpty(materialCode)) {
            throw new Exception("materialCode is empty");
        }
        materialService.updateMaterialDetail(material);
        return ResultVO.success(null);
    }
}
